import { Response } from "express";

export default class ResponseHelper {

  static sendSuccess(result: any, res: Response) {
    res.send(result)
  }

  /**
   * 响应一个错误数据
   * @param error 
   * @param res 
   */
  public static sendError(result: any, res: Response) {
    let err: string;
    if (Array.isArray(result.msg)) {
      err = result.msg.join(";");
    } else {
      err = result.msg
    }

    // 完成响应
    res.send({
      ...result,
      msg: err
    })

  }

  /**
   * 响应客户端请求
   * @param result 
   * @param res 
   */
  public static send(result: any, res: Response) {
    if (result.code === 2000) {
      ResponseHelper.sendSuccess(result, res)
    } else {
      ResponseHelper.sendError(result, res)
    }
  }

}